﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu
{
    public partial class AddMenu : Form
    {



        /// <summary>
        /// ID
        /// </summary>
        public static int Id { get; set; } = 0;

        /// <summary>
        /// 菜单编号
        /// </summary>
        public static string MenuNo { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public static string MenuName { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        public static string MenuType { get; set; }

        /// <summary>
        /// 父级菜单
        /// </summary>
        public static int ParentId { get; set; }


        /// <summary>
        /// 父级菜单名称
        /// </summary>
        public static string ParentName { get; set; }

        /// <summary>
        /// 启用状态
        /// </summary>
        public static int MenuState { get; set; }


        public AddMenu()
        {
            InitializeComponent();

            //查询出所有菜单
            string sql = "select id,MenuName from systemmenu where MenuState=1";

            var menuList = UserOpMysql.ExecuteDataTable(sql); ;
            if (menuList.Rows.Count > 0)
            {
                this.CbxParent.DataSource = menuList;
                this.CbxParent.DisplayMember = "MenuName";
                this.CbxParent.ValueMember = "id";
            }
        }

        private void AddMenu_Load(object sender, EventArgs e)
        {
            if (Id != 0)
            {
                TbxNo.Text = MenuNo;//菜单编号
                TbxName.Text = MenuName;//菜单名称
                CbxType.SelectedText = MenuType;//菜单类型
                CbxParent.SelectedValue = ParentId;//父级菜单
                CbxParent.Text = ParentName;//父级菜单
                CkbStatus.Checked = MenuState == 1;//是否启用
            }
        }

        private void BtnUp_Click(object sender, EventArgs e)
        {
            MenuNo = TbxNo.Text;//菜单编号
            MenuName = TbxName.Text;//菜单名称
            MenuType = CbxType.Text;//菜单类型
            ParentId = Convert.ToInt32(CbxParent.SelectedValue);//父级菜单
            ParentName = CbxParent.Text;//父级菜单
            MenuState = CkbStatus.Checked == true ? 1 : 0;//是否启用



            // 读取配置
            string userName = ConfigurationManager.AppSettings["UserName"];


            if (string.IsNullOrWhiteSpace(MenuNo))
            {
                MessageBox.Show("菜单编号不能为空！", "参数有误！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (string.IsNullOrWhiteSpace(MenuName))
            {
                MessageBox.Show("菜单名称不能为空！", "参数有误！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (string.IsNullOrWhiteSpace(MenuType))
            {
                MessageBox.Show("菜单类型不能为空！", "参数有误！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            string sql = "InsertSystemMenu";

            List<MySqlParameter> mySqlParameters = new List<MySqlParameter>()
            {
                new MySqlParameter("?p_menuNo",MySqlDbType.VarChar){Value=MenuNo},
                new MySqlParameter("?p_menuName",MySqlDbType.VarChar){Value=MenuName},
                new MySqlParameter("?p_menuType",MySqlDbType.VarChar){Value=MenuType},
                new MySqlParameter("?p_parentId",MySqlDbType.Int32){Value=ParentId},
                new MySqlParameter("?p_parentName",MySqlDbType.VarChar){Value=ParentName},
                new MySqlParameter("?p_menuState",MySqlDbType.Int32){Value=MenuState},
                new MySqlParameter("?p_UserName",MySqlDbType.VarChar){Value=userName},
                new MySqlParameter("?p_opid",MySqlDbType.Int32){Value=Id}

            };

            var result = UserOpMysql.ExecuteStoredProcedureToDataTable(sql, mySqlParameters);
            if (result.Rows.Count > 0)
            {
                MessageBox.Show(result.Rows[0]["message"].ToString(), "处理信息！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
        }


    }
}
